package com.github.taomus.gitoys.window;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;

import com.github.taomus.gitoys.config.ConfigManager;
import com.github.taomus.gitoys.config.entity.Config;
import com.github.taomus.gitoys.config.entity.FtpConfig;
import com.github.taomus.gitoys.config.entity.GitConfig;
import com.github.taomus.gitoys.ftp.FtpToy;
import com.github.taomus.gitoys.git.GitToy;
import com.github.taomus.gitoys.git.GitToyServer;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;

public class Main {

	private JFrame frame;
	private JTextField filePath;
	private JTextField ipaddr;
	private JTextField ipPort;
	private JTextField upwd;
	private JTextField uname;
	FtpToy ftp = new FtpToy();
	GitToyServer gserver = new GitToyServer();
	private JTextField gitRemote;
	private JTextField gitSshKey;
	private JTextField gitUname;
	private JTextField gitsPort;
	private JTextField gitbranch;
	private JTextField gitIpAddr;
	private JPasswordField gitUpwd;

	/**
	 * Launch the application.
	 * 
	 * @throws IOException
	 */
	public static void main(String[] args) {
		try {
			Runtime.getRuntime().exec("wmname LG3D");
		} catch (Exception e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 681, 602);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 681, 602 - 20);
		tabbedPane.setToolTipText("");
		frame.getContentPane().add(tabbedPane);
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("FTP Server", panel_1);
		panel_1.setLayout(null);

		filePath = new JTextField();
		filePath.setColumns(10);
		filePath.setBounds(79, 12, 391, 22);
		panel_1.add(filePath);

		JLabel label = new JLabel("文件路径：");
		label.setBounds(12, 14, 79, 18);
		panel_1.add(label);

		JLabel lblIp = new JLabel("IP 地址：");
		lblIp.setBounds(12, 55, 59, 18);
		panel_1.add(lblIp);

		ipaddr = new JTextField();
		ipaddr.setText("127.0.0.1");
		ipaddr.setColumns(10);
		ipaddr.setBounds(67, 46, 114, 22);
		panel_1.add(ipaddr);

		JLabel label_1 = new JLabel("端口 ：");
		label_1.setBounds(186, 55, 59, 18);
		panel_1.add(label_1);

		ipPort = new JTextField();
		ipPort.setText("21");
		ipPort.setColumns(10);
		ipPort.setBounds(230, 46, 48, 22);
		panel_1.add(ipPort);

		upwd = new JTextField();
		upwd.setText("123456");
		upwd.setColumns(10);
		upwd.setBounds(227, 78, 114, 22);
		panel_1.add(upwd);

		JLabel label_3 = new JLabel("密码：");
		label_3.setBounds(186, 85, 59, 18);
		panel_1.add(label_3);

		uname = new JTextField();
		uname.setText("admin");
		uname.setColumns(10);
		uname.setBounds(67, 78, 114, 22);
		panel_1.add(uname);

		JLabel label_2 = new JLabel("用户名：");
		label_2.setBounds(12, 85, 59, 18);
		panel_1.add(label_2);

		JButton button = new JButton("开启");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				FtpConfig ftpConfig = new FtpConfig();
				ftpConfig.setHost(ipaddr.getText().trim());
				ftpConfig.setPassword(upwd.getText().trim());
				ftpConfig.setUsername(uname.getText().trim());
				ftpConfig.setPath(filePath.getText().trim());
				String port = ipPort.getText().trim();
				ftpConfig.setPort(port == null ? 21 : Integer.valueOf(port));
				config.setFtp(ftpConfig);
				ConfigManager.write(config);
				ftp.start();
			}
		});
		button.setBounds(22, 115, 105, 28);
		panel_1.add(button);

		JButton button_1 = new JButton("关闭");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ftp.stop();
			}
		});
		button_1.setBounds(152, 115, 105, 28);
		panel_1.add(button_1);

		JButton button_1_1 = new JButton("保存");
		button_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				FtpConfig ftp = new FtpConfig();
				ftp.setHost(ipaddr.getText().trim());
				ftp.setPassword(upwd.getText().trim());
				ftp.setUsername(uname.getText().trim());
				ftp.setPath(filePath.getText().trim());
				String port = ipPort.getText().trim();
				ftp.setPort(port == null ? 21 : Integer.valueOf(port));
				config.setFtp(ftp);
				ConfigManager.write(config);
			}
		});
		button_1_1.setBounds(287, 115, 105, 28);
		panel_1.add(button_1_1);

		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Git Tools", panel_2);
		panel_2.setLayout(null);
		JCheckBox checkBox = new JCheckBox("使用验证");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 12, 188, 517);
		panel_2.add(scrollPane);

		JTree tree = new JTree();
		scrollPane.setViewportView(tree);

		JLabel label_4 = new JLabel("远程仓库：");
		label_4.setBounds(218, 25, 65, 18);
		panel_2.add(label_4);

		gitRemote = new JTextField();
		gitRemote.setBounds(285, 25, 367, 22);
		panel_2.add(gitRemote);
		gitRemote.setColumns(10);

		ButtonGroup group = new ButtonGroup();
		JRadioButton rdbtnSsh = new JRadioButton("SSH KEY");
		rdbtnSsh.setSelected(true);
		rdbtnSsh.setBounds(369, 55, 93, 26);
		group.add(rdbtnSsh);
		panel_2.add(rdbtnSsh);

		JRadioButton rdbtnHttp = new JRadioButton("HTTP  登陆");
		rdbtnHttp.setBounds(498, 55, 131, 26);
		group.add(rdbtnHttp);
		panel_2.add(rdbtnHttp);

		JLabel lblSshKey = new JLabel("SSH KEY 路径：");
		lblSshKey.setBounds(224, 90, 105, 18);
		panel_2.add(lblSshKey);

		gitSshKey = new JTextField();
		gitSshKey.setBounds(324, 90, 328, 22);
		panel_2.add(gitSshKey);
		gitSshKey.setColumns(10);

		JLabel label_5 = new JLabel("用户名：");
		label_5.setBounds(224, 124, 59, 18);
		panel_2.add(label_5);

		gitUname = new JTextField();
		gitUname.setText("admin");
		gitUname.setBounds(276, 122, 105, 22);
		panel_2.add(gitUname);
		gitUname.setColumns(10);

		JLabel label_6 = new JLabel("密码：");
		label_6.setBounds(234, 156, 59, 18);
		panel_2.add(label_6);

		JTextArea consoleLog = new JTextArea();
		JButton button_2 = new JButton("克隆远程镜像");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				GitConfig gc = new GitConfig();
				gc.setBranch(gitbranch.getText().trim());
				gc.setRemoteRepository(gitRemote.getText().trim());
				gc.setIp(gitIpAddr.getText().trim());
				gc.setPort(Integer.valueOf(gitsPort.getText().trim()));
				String pwd = new String(gitUpwd.getPassword()).trim();
				if (pwd != null && !pwd.equals("")) {
					gc.setPassword(pwd);
				}
				gc.setUsername(gitUname.getText().trim());
				gc.setSshkeypath(gitSshKey.getText().trim());
				for (Component c : panel_2.getComponents()) {
					if (c instanceof JRadioButton) {
						if (((JRadioButton) c).isSelected()) {
							String info = ((JRadioButton) c).getText();
							gc.setIsSshKey(info.equals("SSH KEY"));
						}
					}
				}
				gc.setIsCheck(checkBox.isSelected());
				config.setGit(gc);
				ConfigManager.write(config);
				String log = GitToy.clone(gc);
				consoleLog.setText(log);
			}
		});
		button_2.setBounds(393, 122, 148, 28);
		panel_2.add(button_2);

		JButton button_2_1 = new JButton("更新镜像");
		button_2_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				GitConfig gc = new GitConfig();
				gc.setBranch(gitbranch.getText().trim());
				gc.setRemoteRepository(gitRemote.getText().trim());
				gc.setIp(gitIpAddr.getText().trim());
				gc.setPort(Integer.valueOf(gitsPort.getText().trim()));
				String pwd = new String(gitUpwd.getPassword()).trim();
				if (pwd != null && !pwd.equals("")) {
					gc.setPassword(pwd);
				}
				gc.setUsername(gitUname.getText().trim());
				gc.setSshkeypath(gitSshKey.getText().trim());
				for (Component c : panel_2.getComponents()) {
					if (c instanceof JRadioButton) {
						if (((JRadioButton) c).isSelected()) {
							String info = ((JRadioButton) c).getText();
							gc.setIsSshKey(info.equals("SSH KEY"));
						}
					}
				}
				gc.setIsCheck(checkBox.isSelected());
				config.setGit(gc);
				String log = GitToy.update(gc);
				consoleLog.setText(log);
			}
		});
		button_2_1.setBounds(393, 153, 148, 28);
		panel_2.add(button_2_1);

		JButton button_3 = new JButton("开启服务");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final String port = gitsPort.getText().trim();
				final String ip = gitIpAddr.getText().trim();
				Thread t = new Thread() {
					public void run() {
						gserver.start(ip, port != null ? Integer.valueOf(port) : 9000);
					}
				};
				t.start();
			}
		});
		button_3.setBounds(393, 199, 105, 28);
		panel_2.add(button_3);

		JButton button_3_1 = new JButton("关闭服务");
		button_3_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gserver.stop();
			}
		});
		button_3_1.setBounds(510, 199, 105, 28);
		panel_2.add(button_3_1);

		JLabel label_7 = new JLabel("端口号：");
		label_7.setBounds(218, 230, 59, 18);
		panel_2.add(label_7);

		gitsPort = new JTextField();
		gitsPort.setText("9000");
		gitsPort.setColumns(10);
		gitsPort.setBounds(280, 228, 65, 22);
		panel_2.add(gitsPort);

		JLabel label_8 = new JLabel("分支名：");
		label_8.setBounds(218, 262, 59, 18);
		panel_2.add(label_8);

		gitbranch = new JTextField();
		gitbranch.setText("master");
		gitbranch.setColumns(10);
		gitbranch.setBounds(276, 260, 105, 22);
		panel_2.add(gitbranch);

		JButton button_3_2 = new JButton("将分支同步到远程");
		button_3_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				GitConfig gc = new GitConfig();
				gc.setBranch(gitbranch.getText().trim());
				gc.setRemoteRepository(gitRemote.getText().trim());
				gc.setIp(gitIpAddr.getText().trim());
				gc.setPort(Integer.valueOf(gitsPort.getText().trim()));
				String pwd = new String(gitUpwd.getPassword()).trim();
				if (pwd != null && !pwd.equals("")) {
					gc.setPassword(pwd);
				}
				gc.setUsername(gitUname.getText().trim());
				gc.setSshkeypath(gitSshKey.getText().trim());
				for (Component c : panel_2.getComponents()) {
					if (c instanceof JRadioButton) {
						if (((JRadioButton) c).isSelected()) {
							String info = ((JRadioButton) c).getText();
							gc.setIsSshKey(info.equals("SSH KEY"));
						}
					}
				}
				gc.setIsCheck(checkBox.isSelected());
				config.setGit(gc);
				String log = GitToy.syncRemote(gc);
				consoleLog.setText(log);
			}
		});
		button_3_2.setBounds(393, 257, 148, 28);
		panel_2.add(button_3_2);

		JButton button_4 = new JButton("保存配置");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config config = ConfigManager.read();
				GitConfig gc = new GitConfig();
				gc.setBranch(gitbranch.getText().trim());
				gc.setRemoteRepository(gitRemote.getText().trim());
				gc.setIp(gitIpAddr.getText().trim());
				gc.setPort(Integer.valueOf(gitsPort.getText().trim()));
				String pwd = new String(gitUpwd.getPassword()).trim();
				if (pwd != null && !pwd.equals("")) {
					gc.setPassword(pwd);
				}
				for (Component c : panel_2.getComponents()) {
					if (c instanceof JRadioButton) {
						if (((JRadioButton) c).isSelected()) {
							String info = ((JRadioButton) c).getText();
							gc.setIsSshKey(info.equals("SSH KEY"));
						}
					}
				}
				gc.setIsCheck(checkBox.isSelected());
				gc.setUsername(gitUname.getText().trim());
				gc.setSshkeypath(gitSshKey.getText().trim());
				config.setGit(gc);
				ConfigManager.write(config);
			}
		});
		button_4.setBounds(559, 257, 105, 28);
		panel_2.add(button_4);

		JLabel lblConsole = new JLabel("Console:");
		lblConsole.setBounds(218, 315, 93, 18);
		panel_2.add(lblConsole);

		consoleLog.setBounds(228, 345, 415, 184);
		panel_2.add(consoleLog);

		gitIpAddr = new JTextField();
		gitIpAddr.setText("127.0.0.1");
		gitIpAddr.setColumns(10);
		gitIpAddr.setBounds(280, 199, 101, 22);
		panel_2.add(gitIpAddr);

		JLabel label_7_1 = new JLabel("IP：");
		label_7_1.setBounds(218, 201, 59, 18);
		panel_2.add(label_7_1);

		Config config = ConfigManager.read();
		GitConfig gc = config.getGit();
		gitbranch.setText(gc.getBranch());
		gitRemote.setText(gc.getRemoteRepository());
		gitIpAddr.setText(gc.getIp());
		gitsPort.setText(String.valueOf(gc.getPort()));
		gitUname.setText(gc.getUsername());
		gitSshKey.setText(gc.getSshkeypath());

		gitUpwd = new JPasswordField();
		gitUpwd.setBounds(275, 154, 106, 22);
		panel_2.add(gitUpwd);

	
		checkBox.setSelected(true);
		checkBox.setBounds(231, 55, 114, 26);
		panel_2.add(checkBox);

		uname.setText(config.getFtp().getUsername());
		upwd.setText(config.getFtp().getPassword());
		filePath.setText(config.getFtp().getPath());
		ipaddr.setText(config.getFtp().getHost());
		ipPort.setText(String.valueOf(config.getFtp().getPort()));
	}
}
